#ifndef MODULEHPLCPUMPDEFINE_H
#define MODULEHPLCPUMPDEFINE_H

#include <QVector>
#include "modulehplcdefine.h"

namespace hplc
{

enum PumpMaterial
{
    PumpMaterial_SS = 0, // 316L不锈钢
    PumpMaterial_PTFE,   // (耐压是5MPa)
    // PumpMaterial_PCTFE, // 全聚三氟氯乙烯泵头
    PumpMaterial_PEEK, // 聚醚醚酮      (耐压是SS的一半)
    PumpMaterial_TI,   // TA2钛合金       (耐压和SS一样)
    PumpMaterial_HC,   // C276哈氏合金    (耐压和SS一样)
};

struct PumpMechanicalParam
{
    double          throughput; // 凸轮转一圈的吞吐量
    double          reductionGearRatio; // 减速比

    PumpMaterial 	material; // 泵头材质
};

struct PumpConfigParam
{
    DriverType		driverType; // 驱动器类型

    double			pulseCompenMaxRate; // 最大脉动补偿流速
    quint8			rateDecimal; // 流速的小数点位数

    double          flowMin; // 最小流速
    double          flowMax; // 最大流速
    double          pressMin; // 最小压力
    double          pressMax; // 最大压力
};

enum PumpModel
{
    PumpModel_10ml = 0,
    PumpModel_50ml,
    PumpModel_100ml,
    PumpModel_200ml,
    PumpModel_300ml,
    PumpModel_500ml,
    PumpModel_800ml,
    PumpModel_1000ml,
    PumpModel_3000ml,
};

enum PumpMode
{
    PumpMode_HPGA = 0,
    PumpMode_HPGB,
    PumpMode_HPGC,
    PumpMode_HPGD,
};

enum PumpWorkMode
{
    PumpWorkMode_Normal = 0,
    PumpWorkMode_PressureCalib = 0x01,
    PumpWorkMode_RateCalib = 0x02,
    PumpWorkMode_PulseCompenCalib = 0x04,
    PumpWorkMode_MotorCurrentCalib = 0x08,
};

enum PumpRunMode
{
    PumpRunMode_ConstRate = 0,
    PumpRunMode_ConstPressure1,
    PumpRunMode_ConstPressure2,
};

struct PumpRateCalibData
{
    double			rpm;
    double			rate;

    bool operator==(const PumpRateCalibData &other) const
    {
        return (qFuzzyCompare(rpm, other.rpm) && qFuzzyCompare(rate, other.rate));
    }
};

struct PumpPressureCalibData
{
    double			adc;
    double			pressure;

    bool operator==(const PumpPressureCalibData &other) const
    {
        return (qFuzzyCompare(adc, other.adc) && qFuzzyCompare(pressure, other.pressure));
    }
};

struct PumpPulseCompenCalibData
{
    int				position;
    double			factor;

    bool operator==(const PumpPulseCompenCalibData &other) const
    {
        return (position == other.position && qFuzzyCompare(factor, other.factor));
    }
};

struct PumpStepMotorCurrentCalibData
{
    double			rpm;
    quint8			current;

    bool operator==(const PumpStepMotorCurrentCalibData &other) const
    {
        return (qFuzzyCompare(rpm, other.rpm) && current == other.current);
    }
};

struct PumpLoadCalibParam
{
    double			rate;
    double			realRate;
    double			pressure;
};

struct PumpDeviceParam
{
    PumpModel       model; // 型号

    ComType         comType; // 通信端口
    quint8          com; // 串口号
    NetType         netType; // 网口通信方式
    QString         remoteIP;
    quint16         remotePort;
    QString         localIP;
    quint16         localPort;

    quint8          isConnect;
    quint8          isDisconncetException; // 连接过程中异常断开连接，则重新连接。主要用于TCP Clinet
};

struct PumpBasicParam
{
    double          rate; // ml/min
    quint8          ratePercent; // [1~100]
    double          purgeRate; // ml/min
    double          purgeTime; // min
    double 			pressureMin;
    double			pressureMax;
    double			pressureWarning;

    bool			isStart;
    bool			isPause;
    bool			isPurge;

    quint8			pressureUploadFrequency;
    bool			isUsedPressureCompensation;

    PumpMode		mode;
    double			pressure;
    double			pressureZeroVoltage; // 压力零点电压
    PumpRunMode 	runMode;

    double          currentRpm;
};

struct PID
{
    double p;
    double i;
    double d;
};

struct PressurePidOut
{
    double pressure;
    double pidOut;
};

struct PumpConstPressureParam
{
    double          rateMax;
    double          rateMin;
    double          pressureThreshold;
    double          pressure; // 目标压力
    PID             pid;

    PressurePidOut  current;

    quint8          pressurePidOutUploadFreq;
};

struct PumpSystemParam
{
    quint8       							workMode; // ref enum PumpWorkMode

    QList<PumpRateCalibData>				rateCalibTable;
    PumpLoadCalibParam						loadCalibParam;

    QList<PumpPressureCalibData>			pressureCalibTable;

    QList<PumpPulseCompenCalibData>			pulseCompenCalibTable;

    quint8                                  stepMotorIRun;
    QList<PumpStepMotorCurrentCalibData>	stepMotorCurrentCalibTable;

};

// 泵的所有参数
struct PumpParam : public CommonParam
{
    PumpMechanicalParam			mechanicalParam;
    PumpConfigParam				configParam;

    PumpDeviceParam				deviceParam;
    PumpBasicParam				basicParam;
    PumpSystemParam				systemParam;

    PumpConstPressureParam      constPressureParam;
};

} // namespace hplc
Q_DECLARE_METATYPE(bool)
Q_DECLARE_METATYPE(hplc::PumpMode)
Q_DECLARE_METATYPE(hplc::PumpRunMode)
Q_DECLARE_METATYPE(hplc::PumpModel)
Q_DECLARE_METATYPE(hplc::PumpRateCalibData)
Q_DECLARE_METATYPE(QList<hplc::PumpRateCalibData>)
Q_DECLARE_METATYPE(hplc::PumpPressureCalibData)
Q_DECLARE_METATYPE(QList<hplc::PumpPressureCalibData>)
Q_DECLARE_METATYPE(hplc::PumpPulseCompenCalibData)
Q_DECLARE_METATYPE(QList<hplc::PumpPulseCompenCalibData>)
Q_DECLARE_METATYPE(hplc::PumpStepMotorCurrentCalibData)
Q_DECLARE_METATYPE(QList<hplc::PumpStepMotorCurrentCalibData>)
Q_DECLARE_METATYPE(hplc::PumpLoadCalibParam)
Q_DECLARE_METATYPE(hplc::PID)
Q_DECLARE_METATYPE(hplc::PressurePidOut)

Q_DECLARE_METATYPE(hplc::PumpMechanicalParam)
Q_DECLARE_METATYPE(hplc::PumpConfigParam)
Q_DECLARE_METATYPE(hplc::PumpDeviceParam)
Q_DECLARE_METATYPE(hplc::PumpBasicParam)
Q_DECLARE_METATYPE(hplc::PumpSystemParam)
Q_DECLARE_METATYPE(hplc::PumpConstPressureParam)

Q_DECLARE_METATYPE(hplc::PumpParam)

#endif // MODULEHPLCPUMPDEFINE_H
